
import { Toast } from 'react-vant';

let initdata = {
    arr:[], // 请求数据的数组
    shoparr:[],// 购物车数组,
    flagall:false,
    addressarr:[
        {
            name:"zhan gsan",
            tel:1155555555,
            address:"北京市昌平区"
        }
    ]// 地址数组
}
let shopreducer = (state=initdata,{type,payload})=>{
    switch(type) {
        case "getlist":
            let newARR = JSON.parse(JSON.stringify(state.arr))
            newARR = payload
           
            return {
                ...state,
                arr:newARR
            }
        case "addshop":
             let newshopARR = JSON.parse(JSON.stringify(state.shoparr))
            let findindex =  state.shoparr.findIndex((item,index)=>{
                return item.id == payload.id
            })
            if (findindex !== -1) {
                Toast.info('您已经加入过购物车l')
            } else {
                newshopARR.push(payload)
            }
            return {
                ...state,
                shoparr:newshopARR
            }
        case "addFn":
            // let addshoparr = [...state.shoparr]
          let  addshoparr = state.shoparr.map((item,index)=>{
                if (item.id === payload) {
                    item.num ++
                }
                return item
            })
            return {
                ...state,
                shoparr:addshoparr
            }
        case "delFn":
            // let addshoparr = [...state.shoparr]
            let  delshoparr = state.shoparr.map((item,index)=>{
                if (item.id === payload) {
                    item.num -- 
                    if ( item.num < 1) {
                        item.num =1
                        alert("您不能在减去了")
                    }
                }
                return item
            })
            return {
                ...state,
                shoparr:delshoparr
            }
        case "changeall":
            state.flagall = !state.flagall
            let cheakallshopArr = state.shoparr.map((item,index)=>{
                item.checked =   state.flagall
                return item
            }) 
            return {
                ...state,
                flagall: state.flagall,
                shoparr:cheakallshopArr
            }
        case "checkItem" :
            let cheakitemshopArr = state.shoparr.map((item,index)=>{
                if (item.id === payload) {
                    item.checked = !item.checked
                }
                return item
            }) 
            let flagAll1 = cheakitemshopArr.every((item)=>{
                return item.checked
            })
            return {
                ...state,
                flagall:flagAll1,
                shoparr:cheakitemshopArr
            }
        case "addaddress" :
             let newaddressarr = [...state.addressarr]
             newaddressarr.push(payload)
            return {
                ...state,
                addressarr:newaddressarr
            }
        case "deLFn":
            let newaddressarr1 = [...state.addressarr]
            newaddressarr1.splice(payload,1)
            return {
                ...state,
                addressarr:newaddressarr1
            }
        case "editaddress" :
            let editnewaddressarr = [...state.addressarr]
            editnewaddressarr[payload.editindex] = payload
            return {
                ...state,
                addressarr:editnewaddressarr
            }

        default:
            return {
                ...state
            }
    }
    
}
export default shopreducer